package com.netdisk.service.impl;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.netdisk.dao.impl.CapacityDao;
import com.netdisk.dao.impl.CompanyDao;
import com.netdisk.dao.impl.FileSystemDao;
import com.netdisk.dao.impl.GroupDao;
import com.netdisk.dao.impl.GroupsUsersDao;
import com.netdisk.dao.impl.UserDao;
import com.netdisk.entity.Capacity;
import com.netdisk.entity.Company;
import com.netdisk.entity.FileSystem;
import com.netdisk.entity.Group;
import com.netdisk.entity.GroupsUsers;
import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.Service;

/**
 * 公司Service
 * @author chenlong
 *2013-4-13
 */
@Component("companyService")
public class CompanyService extends Service<Company>{
	
	private CompanyDao companyDao;
	
	private CapacityDao capacityDao;
	
	private GroupDao groupDao ; 
	
	private UserDao userDao;
	
	private GroupsUsersDao groupsUsersDao; 
	
	public GroupsUsersDao getGroupsUsersDao() {
		return groupsUsersDao;
	}
	
	private FileSystemDao fileSystemDao;
	
	@Resource(name="fileSystemDao")
	public void setFileSystemDao(FileSystemDao fileSystemDao) {
		this.fileSystemDao = fileSystemDao;
	}

	@Resource(name="groupsUsersDao")
	public void setGroupsUsersDao(GroupsUsersDao groupsUsersDao) {
		this.groupsUsersDao = groupsUsersDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}
	
	@Resource(name="userDao")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public CapacityDao getCapacityDao() {
		return capacityDao;
	}
	
	@Resource(name="capacityDao")
	public void setCapacityDao(CapacityDao capacityDao) {
		this.capacityDao = capacityDao;
	}

	public GroupDao getGroupDao() {
		return groupDao;
	}
	
	@Resource(name="groupDao")
	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}
	
	@Resource(name="companyDao")
	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}
	
	@Resource(name="companyDao")
	public void setCompany(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
	
	/**
	 * 因为新建一个企业用户的时候
	 * 需要持久化
	 * company , companyLog ,
	 * capacity , companyLog ,
	 * group ,groupLog
	 * 等信息,要在一个事物里面, 所以提供了这么个方法
	 * @param com 公司
	 * @param complog 公司日志
 	 * @param cap 容量
 	 * @param capLog 容量日志
	 * @param group 组
	 * @param groupLog 组日志
	 * @param user 用户
	 * @param userLog 用户日志
	 */

	public void save(Company com, Log compLog, Capacity cap,Log capLog,
			Group group, Log groupLog, GroupsUsers groupUsers,
			User user, Log userLog, FileSystem fileSystem, Log fileLog) {
		this.save(com, compLog);
		
		capacityDao.save(cap);
		logDao.save(capLog);
		
		groupDao.save(group);
		logDao.save(groupLog);
		
		groupsUsersDao.save(groupUsers);
		
		userDao.save(user);
		logDao.save(userLog);
		
		String storeName = fileSystem.getStoreName();
		File f = new File(storeName);
		f.mkdir();// 创建文件夹
		String[] temp = storeName.split("/");
		storeName = temp[temp.length - 1];
		fileSystem.setStoreName(storeName);
		
		fileSystemDao.save(fileSystem);
		logDao.save(fileLog);
	}

	@Override
	public void save(Company obj, Log log) {
		companyDao.save(obj);
		logDao.save(log);
	}

	@Override
	public void update(Company obj, Log log) {
		companyDao.update(obj);
		logDao.save(log);
	}

	@Override
	public void delete(Company obj, Log log) {
		companyDao.delete(obj);
		logDao.save(log);
	}

	@Override
	public List<Company> findAll() {
		return companyDao.findAll();
	}

	@Override
	public Company findById(String id) {
		return companyDao.findById(id);
	}

	@Override
	public List<Company> findByTop(int from, int max) {
		return companyDao.findByTop(from, max);
	}
	
}
